﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;

namespace PT.CarList
{
    partial class EditCarList : PortalModuleBase
    {

        #region Private Members
        public CarListController _objCarController;
        #endregion

        #region Public Methods

        public CarListController objCarController
        {
            get
            {
                if (this._objCarController == null)
                {
                    this._objCarController = new CarListController();
                }
                return this._objCarController;
            }
        }

        public string strState
        {
            get
            {
                return ViewState["strState"] != null ? ViewState["strState"].ToString() : "";
            }
            set { ViewState["strState"] = value; }
        }

        public string strItemId
        {
            get
            {
                return ViewState["strItemId"] != null ? ViewState["strItemId"].ToString() : "";
            }
            set { ViewState["strItemId"] = value; }
        }

        #endregion

        #region Event Handlers
        protected void Page_Load(Object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    List<CarInfo> carList = this.objCarController.GetCarList(this.ModuleId);
                    this.grdCar.DataSource = carList;
                    this.grdCar.DataBind();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void _grdCar_OnNeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            List<CarInfo> carList = this.objCarController.GetCarList(this.ModuleId);
            this.grdCar.DataSource = carList;
        }

        protected void _btnAdd_Click(Object sender, EventArgs e)
        {
            try
            {
                this.txtName.Text = "";
                this.txtOtherInfo.Text = "";
                this.txtPrice.Text = "";
                this.imgCar.ImageUrl = "";

                this.strState = "add";
                this.mvView.SetActiveView(this.viewDetail);
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void _btnSave_Click(Object sender, EventArgs e)
        {
            try
            {
                string strPath = PortalSettings.HomeDirectory + "/productImage/";
                string strURLPath = strPath;
                strPath = Server.MapPath(strPath);
                bool blnHasImg = false;
                if (this.ruUpload.UploadedFiles.Count > 0)
                {
                    strPath = strPath + this.ruUpload.UploadedFiles[0].FileName;
                    strURLPath = strURLPath + this.ruUpload.UploadedFiles[0].FileName;
                    strURLPath = strURLPath.Replace("/", "\\");
                    this.ruUpload.UploadedFiles[0].SaveAs(strPath);
                    blnHasImg = true;
                }
                this.objCarController.InsertUpdateCar(
                    new CarInfo
                    {
                        ModuleId = this.ModuleId,
                        ItemId = this.strState == "add" ? 0 : int.Parse(this.strItemId),
                        CarName = this.txtName.Text.Trim(),
                        CarType = this.txtCarType.Text.Trim(),
                        Price = this.txtPrice.Text.Trim(),
                        ImageURL = blnHasImg ? strURLPath : "",
                        OtherInfo = this.txtOtherInfo.Text,
                        DetailInfo=this.txtDetailInfo.Text,
                        CarManufacture = this.txtManufacture.Text,
                        CreatedByUser = this.UserId
                    });
                this.lblResult.Text = "Lưu thành công!";
                if (blnHasImg)
                {
                    this.imgCar.ImageUrl = strURLPath;
                }

                if (this.strState == "add")
                {
                    List<CarInfo> carList = this.objCarController.GetCarList(this.ModuleId);
                    this.grdCar.DataSource = carList;
                    this.grdCar.DataBind();
                    this.mvView.SetActiveView(this.viewListCar);
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void _btnBack_Click(Object sender, EventArgs e)
        {
            try
            {
                List<CarInfo> carList = this.objCarController.GetCarList(this.ModuleId);
                this.grdCar.DataSource = carList;
                this.grdCar.DataBind();

                this.mvView.SetActiveView(this.viewListCar);
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void _btnEditItem_Click(Object sender, EventArgs e)
        {
            try
            {
                LinkButton btnEditItem = sender as LinkButton;
                this.strItemId = btnEditItem.CommandArgument;
                CarInfo objCar = this.objCarController.GetCar(int.Parse(strItemId));

                this.txtName.Text = objCar.CarName;
                this.txtCarType.Text = objCar.CarType;
                this.txtPrice.Text = objCar.Price;
                this.txtOtherInfo.Text = objCar.OtherInfo;
                this.imgCar.ImageUrl = objCar.ImageURL;
                this.txtManufacture.Text = objCar.CarManufacture;
                this.txtDetailInfo.Text = objCar.DetailInfo;
                this.strState = "save";
                this.mvView.SetActiveView(this.viewDetail);
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void _btnDelete_Click(Object sender, EventArgs e)
        {
            try
            {
                LinkButton btnEditItem = sender as LinkButton;
                this.strItemId = btnEditItem.CommandArgument;
                this.objCarController.DeleteCar(int.Parse(strItemId));

                List<CarInfo> carList = this.objCarController.GetCarList(this.ModuleId);
                this.grdCar.DataSource = carList;
                this.grdCar.DataBind();
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }
}

